//TableEditSubmit_INC.js

function  Formsubmit(Form, ReturnAction) {

	var InputSKUPass = true;
	var InputSKUArr = new Array();
	if(ReturnAction){
		Form.action = Form.action + "/ReturnAction/" +  ReturnAction;
	}

	var inputObject = $("input[data='item_sku']");

	inputObject.each(function(){ 
			if(this.value == '') { 
				InputSKUPass = false; 
				return false;
			}else{
				InputSKUArr.push(this.value);
			}

		}
	);
	
	if(InputSKUPass){
		InputSKUPass = CheckRepeatSKU(InputSKUArr);
	}
	else{
		alert("请准确填写, SKU不能为空。");
		return false;
	}
	//data="standard_price"
	var inputObject = $("input[data='standard_price']");
	var StandardPricePass = true;
	inputObject.each(function(){ 
			if(this.value == '') { 
				$(this).focus().select();
				StandardPricePass = false; 
				return false;
			}
		}
	);
	
	if(!StandardPricePass){
		alert("请准确填写, 原价不能为空。");
		return false;
	}

	//data="quantity"
	var inputObject = $("input[data='quantity']");
	var QuantityPass = true;
	inputObject.each(function(){ 
			if(this.value == '') { 
				$(this).focus().select();
				QuantityPass = false; 
				return false;
			}
		}
	);
	if(!QuantityPass){
		alert("请准确填写, 库存不能为空。");
		return false;
	}

	//data="sale_price"
	var inputObject = $("input[data='sale_price']");
	var SalePricePass = true;
	inputObject.each(function(){ 
			if(this.value != '') { 
				var ID = $(this).attr("id");

				if(ID == "sale_price"){
					if($("#sale_from_date").val() == "" ){
						alert("需要填写特价开始时间。");
						$("#sale_from_date").focus().select();
						SalePricePass = false;
						return false;
					}
					if($("#sale_end_date").val() == "" ){
						alert("需要填写特价结束时间。");
						$("#sale_end_date").focus().select();
						SalePricePass = false;
						return false;
					}

				}
				else{
					if($("#sale_from_date_" + ID).val() == "" ){
						alert("需要填写特价开始时间。");
						$("#sale_from_date").focus().select();
						SalePricePass = false;
						return false;
					}
					if($("#sale_end_date_" + ID).val() == "" ){
						alert("需要填写特价结束时间。");
						$("#sale_end_date").focus().select();
						SalePricePass = false;
						return false;
					}
				
				}
			}
		}
	);

	if(InputSKUPass &&　StandardPricePass &&　QuantityPass && SalePricePass){
		setTimeout(function () {
			Form.submit();
			return true;
		}, 100);
	}
	else{		
		return false;
	}
}

jQuery(document).ready(function () { 
	$('#sale_from_date').simpleDatepicker();
	$('#sale_end_date').simpleDatepicker();
}); 

function CreatSKU(){

	var ParentSKU = $("#item_sku").val();
	if(ParentSKU != ""){
		var ChildsSKU = $("input[data='item_sku'][child='true']");
		var tmpNum  = 0;
		for (i = 0; i< ChildsSKU.length;i++ )
		{
			tmpNum  += 1;
			ChildsSKU[i].value = ParentSKU + "_0" + tmpNum;
		}
	}
	else{
		alert("请填写正确的SKU.");
	}
}

function CloneValue(Inputid)
{
	var ParentValue = $("#" +Inputid).val();
	var ChildsInput = $("input[data='" + Inputid + "']")
	for (i = 0; i< ChildsInput.length;i++ )
	{
		ChildsInput[i].value = ParentValue;
	}
}

function ShowDeleteDiv(image){
	var div = $("#DeleteImg");
	var image = $(image);
	if(div.attr("visible") == "false" && image.attr("src") != ""){
		
		var X = image.offset().top;
		var Y = image.offset().left;
		div.attr("visible", "true");
		div.css({"top":X + 80,"left":Y}).show();

		 $("#DeleteImg").bind("click",function(){
			
			var ImageURL = image.attr("id");
			var ImportProductSheetID = $(image).parent().attr("ImportProductSheetID");
			var ImportProductTransSheetID = $(image).parent().attr("ImportProductTransSheetID");
			var ColorName = $(image).parent().attr("ColorName");

			$.ajax({ 
				type: "POST", 
				url: APP + "/Ajax/DeleteImage", 
				data: {ImageURL: ImageURL, ImportSheetTableID:ImportProductSheetID,ColorName:ColorName,TemplateType:"Shoes"},
				dataType: "text",
				cache:false,
				error: function () { 
					return false;
				}, 
				success: function (data) { 
					image.attr("src", "");
					$("#DeleteImg").hide().attr("visible", "false").unbind("click");
					if(ImageURL == "main_image_url"){
						$("#upload_main_" + ImportProductTransSheetID).uploadify('disable', false);
					}
					else{
						ViceuploadLimit = ViceuploadLimit + 1
						$("#upload_vice_" + ImportProductTransSheetID).uploadify('settings', 'uploadLimit', ViceuploadLimit);
						var upload_vice_view = $("#upload_vice_view_" + ImportProductTransSheetID + "  img");
						var upload_vice_buttondisable = true;
						upload_vice_view.each(function(){ 
							if($(this).attr("src") == "") { 
								upload_vice_buttondisable = false;
								return false;
							}
						});

						$("#upload_vice_" + ImportProductTransSheetID).uploadify('disable', upload_vice_buttondisable);
					}
					

                   
				} 
			}); 
		 });

	}
}

function HiddenDeleteDiv(image){
	var obj = document.elementFromPoint(event.clientX,event.clientY);  
        if(obj && obj.id == "DeleteImg")  
        {  
            $("#DeleteImg").hover(function(){  
                $(this).show();  
            },function(){  
                $(this).hide().attr("visible", "false").unbind("click");  
            });  
        }  
        else  
        {  
            $("#DeleteImg").hide().attr("visible", "false").unbind("click");  
        }  

}

function CheckRepeatSKU(InputSKUArr){
	var s = ","  + InputSKUArr.join(",") + ","; 
	for(var i=0;i<InputSKUArr.length;i++) { 
		if(s.replace("," + InputSKUArr[i]+",","").indexOf("," + InputSKUArr[i]+",")>-1) { 
			alert("SKU中有重复数据 : " + InputSKUArr[i]); 
			return false;
			break;
		}
	}
	return true;
}

